	*********************************************************************************
	*                                                                               *
	*                Election Frequency - Preparing the Dataset     	            *
	*                                                                               *
	*                          Preparing Data from CEE Elections  					*
	*																				*			
	*								                                    			*
	*                                                                               *
	*********************************************************************************
 
	global beg_path `"C:\Users\JohnSmith\Dropbox\Replication_EF\"'  // F - Home
 
	cd `"${beg_path}Data\Analysis 1"'
	use electionsdata_CEE.dta, clear
	fre Length

	/* Identifying simultaneous elections */
	sort Country date
	by date Country, sort: gen nvals = _n == 1
	/* nvals is 1 whenever a value is first in its group and is 0 otherwise */
	count if nvals == 0     // total number of pairs
	tab nvals Country
	sort Country date 
	list Country Type Year Date date nvals


	/* Creating groups */
	
	* generate groups
	egen Group = group(date Country) 
	tab Group

	* Rank 
	generate Rank1 = Type
	recode Rank1 (1 = 2) (2 = 1) (3 = 3) (4 = 7) (5 = 8) (6 = 6) (7 = 5) (8 = 4) (10 = 9), generate(Rank)
	drop Rank1

	* Rank within a group by Type and Coverage
	generate Coverage_pop2 = 1/Coverage_pop  // preparing to sort
	sort Group Coverage_pop2 Rank 		 // sort by Group, Coverage (inversed) and Rank	
	quietly by Group :  gen Rank2 = cond(_N==1,0,_n)
	recode Rank2 (0 =.), generate (Rankgroup) 
	drop Rank2 

	* labels 
	label var Group "Groups by elections date and country"
	label var Rank "Rank based in the elections type hierarchy"
	label var Coverage_pop2 "Inverted population coverage to be used in sort"
	label var Rankgroup "Rank within the group by pop coverage and rank"
	  
	/* Joint elections */
	
	* Re-grouping joint elections
	sum Rankgroup // max 6
	gen joint_el1 = Type[_n+1] if Rankgroup == 1&Rankgroup[_n+1] == 2&Group == Group[_n+1]
	gen joint_el2 = Type[_n+2] if Rankgroup == 1&Rankgroup[_n+2] == 3&Group == Group[_n+2]
	gen joint_el3 = Type[_n+3] if Rankgroup == 1&Rankgroup[_n+3] == 4&Group == Group[_n+3]
	gen joint_el4 = Type[_n+4] if Rankgroup == 1&Rankgroup[_n+4] == 5&Group == Group[_n+4]
	gen joint_el5 = Type[_n+5] if Rankgroup == 1&Rankgroup[_n+5] == 6&Group == Group[_n+5]
	
	* Labels 
	label var joint_el1 "2nd simultaneous elections"
	label var joint_el2 "3rd simultaneous elections"
	label var joint_el3 "4th simultaneous elections"
	label var joint_el4 "5th simultaneous elections"
	label var joint_el5 "6th simultaneous elections"
	
	* Delete 2nd and more 
	drop if Rankgroup > 1 & Rankgroup != .

	* Dummies for joint elections  
	forvalues i=1(1)10  { 
	generate Joint`i' = 1 if `i' == joint_el1 | `i' == joint_el2 | `i' == joint_el3  | `i' == joint_el4 | `i' == joint_el5
	}
	foreach x of varlist Joint1-Joint10 {
	replace `x' = 0 if (`x' >= .)
	}
	drop Joint9    // no type 9
	
	* Labels
	label var Joint1 "Joint Presidential"
	label var Joint2 "Joint Lowerhouse"
	label var Joint3 "Joint Upperhouse"
	label var Joint4 "Joint Referendum"
	label var Joint5 "Joint European"
	label var Joint6 "Joint Regional"
	label var Joint7 "Joint Local"
	label var Joint8 "Joint Municipal"
	label var Joint10 "Joint Others"

	save electionsdata_CEE_2.dta, replace



